package Server;

import Data.ClinetData;
import Data.DataPort;
import Data.FileData;
import DataControl.ServerDataSender;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 执行命令的类
 */
public class DoCommend {
    private String NowPath = "F:/"; // 当前目录
    private DataControl.ServerDataSender ServerDataSender=new ServerDataSender();// 服务器数据发送器
    private DataControl.ClientDataResolver clientDataResolver =new DataControl.ClientDataResolver(NowPath);// 客户端数据处理器
    private Socket clientSocket; // 客户端套接字
    private ObjectOutputStream outputStream;;// 当前输出流
    public DoCommend() {
    }

    /**
     * 服务器执行命令动作
     * @param clientData
     */
    public void DoCommend(ClinetData clientData, Socket clientSocket) {
        this.clientSocket = clientSocket;// 获取客户端套接字
        String commend = clientData.Command;// 获取命令
        // 有参数
        if (clientData.Param1!=null){
            String param1 = clientData.Param1;
            switch (commend) {
                case "cd":
                    doCd(param1);
                    break;
                case "xz":
                    doDown(param1);
                    break;
                case "../":
                    doUp(param1);
                    break;
                default:
                    // 可以在这里添加默认处理逻辑
                    break;
            }
        }
        // 没有参数
        else {
            switch (commend) {
                case "ls":
                    doLs();
                    break;
                case "pwd":
                    doPwd();
                    break;
                default:
                    // 可以在这里添加默认处理逻辑
                    break;
            }
        }
    }

    private void doPwd() {
    }

    private void doUp(String param1) {
        this.NowPath = param1;
        System.out.println("服务器执行类：执行../命令，进入的文件夹为："+this.NowPath);
        doLs();
    }

    /**
     * 向客户端发送当前文件夹内的文件夹列表和文件列表
     */
    private void doLs() {
        try {
            System.out.println("服务器执行类：执行ls命令，当前文件夹为："+this.NowPath);
            DataPort serverData = ServerDataSender.getFolderData(NowPath);
            outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
            outputStream.writeObject(serverData);
            //outputStream.flush();
        }catch (IOException e){
            System.out.println("服务器执行类：ls命令执行失败");
            System.out.println(e);
            System.exit(11);
        }
    }

    private void doDown(String param1) {
        try{
            FileData NowFileData= (FileData) ServerDataSender.getFileData(param1);
            outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
            outputStream.writeObject(NowFileData);
            //outputStream.flush();
        }catch (IOException e){
            System.out.println("服务器执行类：xz命令执行失败");
            System.out.println(e);
            System.exit(12);
        }


    }

    /**
     * 执行cd命令
     * @param param1
     */
    private void doCd(String param1) {
        this.NowPath = param1;
        System.out.println("服务器执行类：执行cd命令，进入的文件夹为："+this.NowPath);
        doLs();
    }
}
